import API from '../../utils/api.js'
const app = getApp()
var startX, endX;

var moveFlag = true; // 判断执行滑动事件
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 1,
    select: false,
    tihuoWay: '已完结',
    statusBarHeight: app.globalData.statusBarHeight,
    navList: ['疾病', '检验', '护理', '喂养', '驯养'],
    cur: 0,
    left: '6%',
    questionList: [],
    selectctive: 1,
    isShowDailog: false,
    showArea: true,
    themeList: ['综合', '最新', '最多人感兴趣'],
    isShowCon: true,
    isNav: false,
    isBoxShadow: true,
    page: 1,
    type: 1,
    noData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.questionList(1, 1)
  },
  //发布问题
  release:function(){
    wx.navigateTo({
      url: '/pages/photos/photos',
    })
  },
  touchStart: function (e) {
    startX = e.touches[0].pageX; // 获取触摸时的原点
    moveFlag = true;
  },
  // 触摸移动事件
  touchMove: function (e) {
    endX = e.touches[0].pageX; // 获取触摸时的原点
    if (moveFlag) {
      if (endX - startX > 50) {
        console.log("向左滑动");
        moveFlag = false;
        if (this.data.cur == 0) {
          console.log(1)
          return
        }
        let cur = --this.data.cur
        if (cur == 0) {
          this.setData({
            left: "6%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 1)
        }
        if (cur == 1) {
          this.setData({
            left: "26%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 2)
        }
        if (cur == 2) {
          this.setData({
            left: "46%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 3)
        }
        if (cur == 3) {
          this.setData({
            left: "66%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
        }
        if (cur == 4) {
          this.setData({
            left: "86%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
        }

      }
      if (startX - endX > 50) {
        console.log("向右滑动");
        moveFlag = false;
        if (this.data.cur == 4) {
          return
        }
        let cur = ++this.data.cur
        if (cur == 0) {
          this.setData({
            left: "6%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 1)
        }
        if (cur == 1) {
          this.setData({
            left: "26%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 2)
        }
        if (cur == 2) {
          this.setData({
            left: "46%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 3)
        }
        if (cur == 3) {
          this.setData({
            left: "66%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
          this.questionList(1, 3)
        }
        if (cur == 4) {
          this.setData({
            left: "86%",
            cur: cur,
            isShowDailog: false,
            type: 2
          })
        }
      }
    }
  },
  // 触摸结束事件
  touchEnd: function (e) {
    moveFlag = true; // 回复滑动事件
  },


  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  mySelect(e) {
    var name = e.currentTarget.dataset.name
    var id = e.currentTarget.dataset.id
    this.setData({
      tihuoWay: name,
      select: false,
      id: id
    })
  },
  back() {
    wx.navigateBack({
      delta: 1
    })
  },
  toDetail(e) {

    console.log(e)
    wx.navigateTo({
      url: '/pages/questionDetail/questionDetal?id=' + e.currentTarget.dataset.id,
    })
  },
  questionList(page, type) {
    var data = {
      page: page,
      limit: 10,
      type: type,
      latitude: wx.getStorageSync('lat'),
      longitude: wx.getStorageSync('lon')
    }
    API.answer(data).then(res => {
      console.log(res)
      var questionList = this.data.questionList
      if (res.ret == 0 && res.data.length > 0) {
        this.setData({
          questionList: res.data
          //questionList: questionList.concat(res.data)
        })
      } else {
        this.setData({
          noData: true
        })
      }
    })
  },
  selectMenu(e) {
    var type = e.currentTarget.dataset.type
    var showArea = true
    var selectctive = 1
    if (type == 1) {
      selectctive = 1
      showArea = true
    } else {
      selectctive = 2
      showArea = false
    }
    this.setData({
      selectctive: selectctive,
      showArea: showArea,
      isNav: false,
      isShowCon: true
    })
  },
  slelectTheme(e) {
    var index = e.currentTarget.dataset.index

  },
  slider4change(e) {
    console.log(e.detail.value)
  },
  screen() {
    this.setData({
      isShowCon: !this.data.isShowCon,
      isNav: true,
      isBoxShadow: false,
      isNav: true,
      isBoxShadow: !this.data.isBoxShadow
    })
  },
  done() {

  },
  tab(e) {
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    this.setData({
      isNav: false,
      questionList: []

    })
    if (index == 0) {
      this.setData({
        left: "6%",
        cur: index,
        isShowDailog: false,
        type: 1
      })
      this.questionList(1, 1)
    } else if (index == 1) {
      this.setData({
        left: "26%",
        cur: index,
        isShowDailog: false,
        type: 2
      })
      this.questionList(1, 2)
    } else if (index == 2) {
      this.setData({
        left: "46%",
        cur: index,
        isShowDailog: false,
        type: 2
      })
      this.questionList(1, 2)
    } else if (index == 3) {
      this.setData({
        left: "66%",
        cur: index,
        isShowDailog: false,
        type: 2
      })
      this.questionList(1, 2)
    } else {
      this.setData({
        left: "86%",
        cur: index,
        isShowDailog: false,
        isNav: false,
        isShowCon: true,
        isBoxShadow: true,
        type: 3
      })
      this.questionList(1, 3)
    }

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  onReachBottom: function () {
    if (this.data.type == 3) {
      return
    }
    var page = this.data.page
    page++
    this.questionList(page, this.data.type)
    this.setData({
      page: page
    })
  },

})